package me.numark.activities;

import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import butterknife.Bind;
import me.numark.MainActivity;
import me.numark.R;
import me.numark.utils.Constants;
import me.numark.utils.Global;
import me.numark.utils.Settings;

/**
 * Created by Evan on 15/7/29.
 */
public class SettingsActivity extends BaseActivity implements View.OnClickListener {

    @Bind(R.id.settings_list)
    LinearLayout mList;

    @Bind(R.id.quit_button)
    Button quitButton;

    public static final String EXTRA_FROM_SETTING = "from_setting";

    final static int[] titles = new int[]{R.string.setting_profile, R.string.setting_accounts,
            R.string.setting_msg,
            R.string.setting_friends, R.string.setting_chg_pwd,
            R.string.setting_about};
    final static Class[] activities = new Class[]{EditProfileActivity.class, AccountsActivity.class,
            MsgRemindActivity.class,
            AddFriendsActivity.class,ModifyPasswdActivuty.class,
            AboutUsActivity.class};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        activity_layout_res = R.layout.activity_settings;
        super.onCreate(savedInstanceState);
        quitButton.setOnClickListener(this);
        titleTextView.setText(R.string.settings);
        for (int i = 0; i < titles.length; i++) {
            addLine(i);
        }
    }

    void addLine(int index) {
        View line = getLayoutInflater().inflate(R.layout.settings_list_item, mList, false);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        TextView tv = (TextView) line.findViewById(R.id.title);
        tv.setText(titles[index]);
        line.setId(100 + index);
        line.setOnClickListener(this);
        mList.addView(line, lp);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() >= 100 && v.getId() <= 100 + activities.length) {
            int index = v.getId() - 100;
            if (activities[index] == null) {
                Toast.makeText(this, "还未完成", Toast.LENGTH_SHORT).show();
                return;
            }
            Intent intent = new Intent(this, activities[index]);
            intent.putExtra(EXTRA_FROM_SETTING, true);
            startActivity(intent);
            return;
        }
        switch (v.getId()) {
            case R.id.quit_button:
                quitAccount();
                break;
        }
    }

    void quitAccount() {
        final String[] uids = Settings.getUids();
        /*new AlertDialog.Builder(this)
                .setMessage(uids.length <= 1 ? R.string.dialog_quit_account1 : R.string.dialog_quit_account2)
                .setNegativeButton(android.R.string.cancel, null)
                .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        int uid = Global.getCurrentUid();
                        Settings.delUser(String.valueOf(uid));
                        if (uids.length <= 1) {
                            Settings.setCurrentUid(0);
                            Global.currentUser = null;
                        } else {
                            for (int i = 0; i < uids.length; i++) {
                                String u = uids[i];
                                int id = Integer.valueOf(u);
                                if (id != uid) {
                                    Settings.setCurrentUid(id);
                                    Global.currentUser = Settings.getCurrentUser();
                                    break;
                                }
                            }
                        }
                        LocalBroadcastManager.getInstance(getBaseContext())
                                .sendBroadcast(new Intent(Constants.ACTION_USER_CHANGED));
                        finish();
                    }
                })
                .show();*/
        final Dialog lDialog = new Dialog(this,R.style.dialog_style);
        lDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        lDialog.setContentView(R.layout.r_okcanceldialogview);
        TextView dialogMessage = (TextView) lDialog.findViewById(R.id.dialog_message);
        dialogMessage.setText(uids.length <= 1 ? getString(R.string.dialog_quit_account1) : getString(R.string.dialog_quit_account2));
        ((Button) lDialog.findViewById(R.id.cancel))
                .setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        lDialog.dismiss();
                    }
                });
        ((Button) lDialog.findViewById(R.id.ok))
                .setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        lDialog.dismiss();
                        int uid = Global.getCurrentUid();
                        Settings.delUser(String.valueOf(uid));
                        if (uids.length <= 1) {
                            Settings.setCurrentUid(0);
                            Global.currentUser = null;
                           // Global.currentUser = userHashMap.get(String.valueOf(uid));
                            LocalBroadcastManager.getInstance(getBaseContext())
                                    .sendBroadcast(new Intent(Constants.ACTION_USER_CHANGED));

                            startActivity(new Intent(SettingsActivity.this,MainActivity.class));
                            finish();
                        } else {
                            for (int i = 0; i < uids.length; i++) {
                                String u = uids[i];
                                int id = Integer.valueOf(u);
                                if (id != uid) {
                                    Settings.setCurrentUid(id);
                                    Global.currentUser = Settings.getCurrentUser();
                                    break;
                                }
                            }
                            LocalBroadcastManager.getInstance(getBaseContext())
                                    .sendBroadcast(new Intent(Constants.ACTION_USER_CHANGED));
                            finish();
                        }


                    }
                });
        lDialog.show();
    }
}
